<?php
session_start();
ob_start();
include("function.php");
condb();
$course_id=$_GET['course_id'];
$lesson_id=$_GET['lesson_id'];
require('fpdf/fpdf.php');

$lesson=mysql_fetch_assoc(mysql_query("select * from lesson a,course b where lesson_id='$_GET[lesson_id]' and a.course_id=b.course_id"));

class PDF extends FPDF
{
	// Page header
	function Header()
	{
		global $lesson;
		$this->SetFont('THSarabun','B',14);
		$this->SetY(2);
		$this->Cell(190,5,u2t("ผลการสอบรายวิชา $lesson[course_name] $lesson[lesson_name]"),0,1,'C');
		$this->SetFont('THSarabun','',14);
		$this->Cell(190,5,u2t("เมื่อ ".date('d/m/',strtotime($lesson['exam_stop'])).(date('Y',strtotime($lesson['exam_stop']))+543).date(' H.i น.',strtotime($lesson['exam_stop']))),0,1,'C');
		$this->Cell(190,5,u2t("(คะแนนเต็ม $lesson[question_num])"),0,1,'C');
		$this->Ln(1);
		//$this->HeadForm();
	}
	// Page footer
	function Footer()
	{
		$this->SetFont('THSarabun','',12);
		$this->SetY(-10);
		$this->SetX(5);
		$this->Cell(190,5,u2t('พิมพ์เมื่อ : '.date('d/m/').(date('Y')+543).date(' H.i น.').'    หน้า  : '.$this->PageNo().'/{nb}'),0,1,'R');
	}
	function HeadForm(){
		global $row,$org;
		$this->SetFont('THSarabun','B',18);
		$this->Cell(190,5,u2t('ใบเสนอราคา'),0,1,'C');
		$this->Ln(2);
		$start_x = $x = $this->GetX();
		$start_y = $y = $this->GetY();
		$row_height = 5;
		$w=array(20,100,15,55);
		$number_of_lines=0;
		
		$i=0;
		$this->SetFont('THSarabun','B',12);
		$this->SetXY($x,$y);
		$this->MultiCell($w[$i],$row_height,u2t("เรื่อง"),0,'L');
		$x=$x+$w[$i];
		
		++$i;
		$this->SetXY($x,$y);
		$this->SetFont('THSarabun','',12);
		$this->MultiCell($w[$i],$row_height,u2t($row['quotation_subject']),0,'L');
		$x=$x+$w[$i];
		$number_of_lines += $this->NumLine(u2t($row['quotation_subject']),$w[$i]);
		
		++$i;
		$this->SetFont('THSarabun','B',12);
		$this->SetXY($x,$y);
		$this->MultiCell($w[$i],$row_height,u2t("วันที่"),0,'L');
		$x=$x+$w[$i];
		
		++$i;
		$this->SetXY($x,$y);
		$this->SetFont('THSarabun','',12);
		$this->MultiCell($w[$i],$row_height,u2t(thaidateformat($row['quotation_date'])),0,'L');
		$x=$x+$w[$i];
		
		$x=$this->GetX();
		$y=$this->GetY();
		
		$i=0;
		$this->SetFont('THSarabun','B',12);
		$this->SetXY($x,$y);
		$this->MultiCell($w[$i],$row_height,u2t("ชื่อลูกค้า"),0,'L');
		$x=$x+$w[$i];
		
		++$i;
		$this->SetXY($x,$y);
		$this->SetFont('THSarabun','',12);
		$this->MultiCell($w[$i],$row_height,u2t($org['org_billingname']),0,'L');
		$x=$x+$w[$i];
		$number_of_lines += $this->NumLine(u2t($org['org_billingname']),$w[$i]);
		
		++$i;
		$this->SetFont('THSarabun','B',12);
		$this->SetXY($x,$y);
		$this->MultiCell($w[$i],$row_height,u2t("โทรศัพท์"),0,'L');
		$x=$x+$w[$i];
		
		++$i;
		$this->SetXY($x,$y);
		$this->SetFont('THSarabun','',12);
		$this->MultiCell($w[$i],$row_height,u2t($org['org_tel']),0,'L');
		$x=$x+$w[$i];
		
		$x=$this->GetX();
		$y=$this->GetY();
		
		$i=0;
		$this->SetFont('THSarabun','B',12);
		$this->SetXY($x,$y);
		$this->MultiCell($w[$i],$row_height,u2t("ที่อยู่"),0,'L');
		$x=$x+$w[$i];
		
		++$i;
		$this->SetXY($x,$y);
		$this->SetFont('THSarabun','',12);
		$this->MultiCell($w[$i],$row_height,u2t($org['org_address']),0,'L');
		$x=$x+$w[$i];
		$number_of_lines += $this->NumLine(u2t($org['org_address']),$w[$i]);
		
		++$i;
		$this->SetFont('THSarabun','B',12);
		$this->SetXY($x,$y);
		$this->MultiCell($w[$i],$row_height,u2t("โทรสาร"),0,'L');
		$x=$x+$w[$i];
		
		++$i;
		$this->SetXY($x,$y);
		$this->SetFont('THSarabun','',12);
		$this->MultiCell($w[$i],$row_height,u2t($org['org_fax']),0,'L');
		$x=$x+$w[$i];
		
		$x=$this->GetX();
		$y=$this->GetY();
		
		$i=0;
		$this->SetFont('THSarabun','B',12);
		$this->SetXY($x,$y);
		$this->MultiCell($w[$i],$row_height,u2t("กำหนดยืนราคา"),0,'L');
		$x=$x+$w[$i];
		
		++$i;
		$this->SetXY($x,$y);
		$this->SetFont('THSarabun','',12);
		$this->MultiCell($w[$i],$row_height,u2t("15 วัน นับจากวันที่ออกใบเสนอราคา"),0,'L');
		$x=$x+$w[$i];
		$number_of_lines += $this->NumLine(u2t("15 วัน นับจากวันที่ออกใบเสนอราคา"),$w[$i]);
		
		++$i;
		$this->SetFont('THSarabun','B',12);
		$this->SetXY($x,$y);
		$this->MultiCell($w[$i],$row_height,u2t(""),0,'L');
		$x=$x+$w[$i];
		
		++$i;
		$this->SetXY($x,$y);
		$this->SetFont('THSarabun','',12);
		$this->MultiCell($w[$i],$row_height,u2t(""),0,'L');
		$x=$x+$w[$i];
		//วาดเส้น
		$this->SetXY($start_x,$start_y);
		$this->MultiCell(190,($number_of_lines)*$row_height,'  ',1,'L');
		$this->Ln(4);
		
	}
	function DrawHeader($width,$align,$header){
		//Colors, line width and bold font
		$this->SetFillColor(230);
		$this->SetTextColor(0);
		$this->SetDrawColor(0);
		//$this->SetLineWidth(.3);
		$this->SetFont('THSarabun','B',12);
		
		//Column widths
		$w=$width;
		$x = $this->GetX();
		$y = $this->GetY();
		$row_height = 5;
		//Header
		for($i=0;$i<count($header);$i++){
			$this->MultiCell($w[$i],$row_height*1.5,u2t($header[$i]),1,'C',true);
			$x=$x+$w[$i];
			$this->SetXY($x,$y);
		}
		$this->Ln();
	}
	//Better table
	function DrawData($width,$align,$header,$data,$main_col)
	{
		//Column widths
		$w=$width;
		$x = $this->GetX();
		$y = $this->GetY();
		$row_height = 5;
		
		$this->DrawHeader($width,$align,$header);
		
		//Data
		$i=0;
		if($data!=''){
		foreach($data as $row)
		{
			
			$x = $this->GetX();
			$y = $this->GetY();
			$column_width=$w[$main_col];
						
			$detail=u2t($row[$main_col]);
			$number_of_lines=$this->NumLine($detail,$column_width);
			
			if($this->GetY()+($number_of_lines*$row_height)>280){
				$this->AddPage();
				$this->DrawHeader($width,$align,$header);
				$x = $this->GetX();
				$y = $this->GetY();
			}
			$this->SetFont('THSarabun','',12);
			for($i=0;$i<count($row);++$i){
				$this->SetXY($x,$y);
				if($i!=count($row)-1)
				{
					if($i==$main_col){
						$this->MultiCell($w[$i],$row_height,u2t($row[$i]),0,$align[$i]);
						$this->SetXY($x,$y);
						$this->MultiCell($w[$i],($number_of_lines)*$row_height,'  ',1);
					}
					else{
						$this->MultiCell($w[$i],$row_height,u2t($row[$i]).$this->nl($number_of_lines),0,$align[$i]);
						$this->SetXY($x,$y);
						$this->MultiCell($w[$i],($number_of_lines)*$row_height,'  ',1);
					}
				}
				else
				{
					$this->Cell(8);
					if($row[$i]=='')
					{
					}
					else if($row[$i]==0)
						$this->Image('images/icon_pc.png');
					else
						$this->Image('images/icon_android.png');
					$this->SetXY($x,$y);
					$this->MultiCell($w[$i],($number_of_lines)*$row_height,'  ',1);
				}
				$x=$x+$w[$i];	
			}
			++$i;
		}
		}
		//Closure line
	}
	function nl($num){
		for($i=0;$i<$num;++$i){
			$res.="\n";
		}
		return $res;
		
	}
	function NumLine($detail,$column_width){
		$number_of_lines=0;
		if(substr_count($detail,"\n")<1){
			$number_of_lines = ceil( $this->GetStringWidth($detail) / ($column_width - 1) );
		}else{
			$lines=explode("\n",$detail);
			foreach($lines as $line){
				$number_of_lines += ceil( $this->GetStringWidth($line) / ($column_width - 1) );
			}
		}
		if($number_of_lines==0)$number_of_lines=1;
		return $number_of_lines;
	}
}
function u2t($input){
	return iconv("UTF-8","TIS-620",$input);
}

// Instanciation of inherited class
$pdf = new PDF();
$pdf->AddFont('THSarabun','','THSarabun.php');
$pdf->AddFont('THSarabun','B','THSarabun Bold.php');
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(true,0);
$pdf->AddPage();

//Data loading
$width=array(15,25,50,50,20,20);//ทั้งหมด 190
$align=array('C','L','L','L','R','C');
$header=array('ลำดับที่','รหัส','ชื่อ','สาขาวิชา',"คะแนน",'ทดสอบผ่าน');

$res=mysql_query("select * from course_regis a, lesson b, member c,branch d where a.course_id=b.course_id and b.lesson_id='$_GET[lesson_id]' and a.m_id=c.m_id and c.branch_id=d.branch_id order by c.branch_id,c.m_code");
$i=0;
while($row=mysql_fetch_assoc($res))
{
	++$i;
	$exam = mysql_fetch_assoc(mysql_query("select * from examination where m_id='$row[m_id]' and lesson_id='$_GET[lesson_id]'"));
	$data[]=array($i,$row['m_code'],$row['m_name'],$row['branch_name'],number_format($exam['ex_score']),$exam['ex_from']);
}

$pdf->DrawData($width,$align,$header,$data,1);
$pdf->Output('score.pdf','D');
?>